/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package smaragJava;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;

/**
 *
 * @author northpole
 */
public class PostDAO implements PostDAOInterface {

	
	Connector con;
	//Media media;
	
	public PostDAO(Connector con){
		this.con = con;
	}
	
   public void insert(Post post){
	   
	   String query;
		Statement s;
		String [] values = {post.getAuthor_id().toString(), post.getHot_species().toString()};
		
		query = con.abstractInsert("posts", values);
		
		
		String[] mvalues = { post.getSpecies_id().toString(), post.getId().toString(), post.getAuthor_id().toString()};
		
		try{
			s = con.getConnection().createStatement();
			s.executeUpdate(query);
			
			query = con.abstractInsert("media", mvalues);
			s.executeUpdate(query);
			s.close();
		}
		catch(SQLException e){
			e.printStackTrace();
		}

   }
//    throws CustomerDAOException;

   public void update(Post id/*, User id*/){
    throw new UnsupportedOperationException("Not yet implemented");
   }
  // throws CustomerDAOException;

  public void delete(Post post/*, User ,id*/){
	  
	  String[] fields = { "post_id" };
		String[] postid = {post.getId().toString()};
		String query;
		Statement s;
		
		query = con.abstractDelete(fields, postid, "post_id", "posts");
		
		try{
			s = con.getConnection().createStatement();
			s.executeUpdate(query);
			
			query = con.abstractDelete(fields, postid, "post_id", "media");
			s.executeUpdate(query);
			s.close();
			
		}catch( SQLException e){
			e.printStackTrace();
		}
		
  }

    public User[] findAll(){
     throw new UnsupportedOperationException("Not yet implemented");
   }
   // throws CustomerDAOException;

   public User findByPrimaryKey(String id){
        throw new UnsupportedOperationException("Not yet implemented");
   }
  // throws CustomerDAOException;

  public User[] findByAuthorid(String authorid){
   throw new UnsupportedOperationException("Not yet implemented");
  }
  
}
